package com.ssm.controller;

import com.ssm.entity.Files;
import com.ssm.service.FilesServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/fc")
public class FileController extends BaseController {

    @Resource(name = "filesServiceI")
    private FilesServiceI fsi;

    @RequestMapping("/allFile")
    public String allFile(){
        return prefix + "/file/allFile"+suffix;
    }
    @RequestMapping("/allImg")
    public String allImg(){
        return prefix + "/file/image"+suffix;
    }
    @RequestMapping("/fileTree")
    public String fileTree(HttpServletRequest req){
        String mid = req.getParameter("mid");
        if(mid!=null){
            req.setAttribute("mid",mid);
        }
        return prefix + "/file/fileTree"+suffix;
    }

    @RequestMapping("/find")
    @ResponseBody
    public List<Files> find(Files f){
        return fsi.find(f);
    }

    @RequestMapping("/createDir")
    @ResponseBody
    public Map<String,Object> createDir(HttpServletRequest req){
        String dirName = req.getParameter("dirName");
        String parentId = req.getParameter("parentId");
        return fsi.createDir(dirName,parentId);
    }

    @RequestMapping("/remove/{id}")
    @ResponseBody
    public Map<String,Object> remove(@PathVariable(value = "id") int fid){
        return fsi.remove(fid);
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(Files files){
        return fsi.edit(files);
    }

    @RequestMapping("/uploadFile/{id}")
    @ResponseBody
    public Map<String,Object> uploadFile(
            @PathVariable(value = "id") int id,
            @RequestParam(required = false,value = "fileResource")MultipartFile files[]){
        return fsi.saveFile(files,id);
    }

    @RequestMapping("/uploadDir/{id}")
    @ResponseBody
    public Map<String,Object> uploadDir(
            @PathVariable(value = "id") int id,
            @RequestParam(required = false,value = "dirResource")MultipartFile files[]){
        return fsi.saveDir(files,id);
    }

    @RequestMapping("/downloadData/{id}")
    public void downloadData(@PathVariable(value = "id") int id, HttpServletResponse res){
        fsi.download(id,res);
    }
}
